package com.wc.controller;

import com.wc.pojo.Project;
import com.wc.service.ProjectService;
import com.wc.util.Information;
import com.wc.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目控制层
 */
@RestController
public class ProjectController {

    @Autowired
    private ProjectService service;

    protected static Logger logger = LoggerFactory.getLogger(ProjectController.class);

    @RequestMapping("addProject")
    public Map<String,Boolean> addProject(Project project){
        Map<String,Boolean> map = new HashMap<>();
        System.err.println("addProject===>前端参数===>"+project);
        boolean result = service.addProject(project);
        if (result){
            map.put("result",true);
            logger.info(Information.ADD_SUCCESS);
        }else {
            map.put("result",false);
            logger.info(Information.ADD_FAIL);
        }
        return map;
    }

    @RequestMapping("updateProject")
    public Map<String,Boolean> updateProject(Project project){
        Map<String,Boolean> map = new HashMap<>();
        logger.info("updateProject===>前端参数===>"+project);
        boolean result = service.updateProject(project);
        if (result){
            map.put("result",true);
            logger.info(Information.UPDATE_SUCCESS);
        }else {
            map.put("result",false);
            logger.info(Information.UPDATE_FAIL);
        }
        return map;
    }

    @RequestMapping("removeProject")
    public Map<String,Boolean> removeProject(String bmDm){
        Map<String,Boolean> map = new HashMap<>();
        logger.info("removeProject===>前端参数===>"+bmDm);
        boolean result = service.removeProject(bmDm);
        if (result){
            map.put("result",true);
            logger.info(Information.DELTE_SUCCESS);
        }else {
            map.put("result",false);
            logger.info(Information.DELTE_FAIL);
        }
        return map;
    }

    @RequestMapping("getChildren")
    public List<Project> getChildren(String xmDm){
        logger.info("getChildren===>前端参数===>"+xmDm);
        return service.getChildren(xmDm);
    }

    @RequestMapping("getShow")
    public List<Project> getShow(){
        return service.getShow();
    }

    @RequestMapping("verificationOwnerid")
    public Map<String,Boolean> verificationOwnerid(@RequestParam(value = "xmOwnerid")Integer xmOwnerid){
        Map<String,Boolean> map = new HashMap<>();
        logger.info("verificationOwnerid===>前端参数===>"+xmOwnerid);
        Project result = service.getOwnerid(xmOwnerid);
        if (StringUtils.isEmpty(result)){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;
    }

    @RequestMapping("getProjectbm")
    public Project getProjectbm(@RequestParam(value = "bmDm") String bmDm){
        logger.info("getProjectbm===>前端参数===>"+bmDm);
        return service.getBmdm(bmDm);
    }

}
